% compare predictive sequence match scores for match vs nonmatch conditions using PSTATES, the
% output of analyzeSequence(..), which must be called first, followed by 
%  global STATES 
% to make it accessible.
% 
% pool_start_bins: matrix: rows are all of the start bins for pattern, row,
%  that will be pooled.  The number of rows = the number of patterns (4).
%  Start bins start at 1, the first bins_per_second bins in PSTATES.
%  All the pool windows will be alligned.
%
% pool_length_seconds: how many consecutive seconds within each trial do you wish
%  to pool?
%
% pooled_averages = plot_georgopoulos(PSTATES, pool_start_bins, stimulus_numbers, pool_length_in_bins );
%
function data = plot_georgopoulos(PSTATES, pool_start_bins, stimulus_numbers, pool_length_in_bins )

n_patterns = size(pool_start_bins, 1);


figure
set(gcf, 'color', 'blue');
set(gca, 'color', 'yellow');
set(gcf, 'InvertHardCopy', 'off');

%prepare data
alligned_sum = zeros(n_patterns,pool_length_in_bins);
for pool_number = 1:n_patterns
    for trial = 1:size(pool_start_bins,2)
        % shift the data
        alligned_sum(pool_number,:)=alligned_sum(pool_number,:) + PSTATES(stimulus_numbers(pool_number,trial),pool_start_bins(pool_number,trial):pool_start_bins(pool_number,trial)+pool_length_in_bins-1);
    end
end

% average
data = alligned_sum*(1/size(pool_start_bins,2));


%%
% Plot

figure
set(gcf, 'color', 'blue');
set(gca, 'color', 'yellow');
set(gcf, 'InvertHardCopy', 'off');
hold off;
plot(data(1,:),'y','LineWidth',3);
hold on;
plot(data(2,:),'g','LineWidth',3);
plot(data(3,:),'r','LineWidth',3);
plot(data(4,:),'c','LineWidth',3);
%plot(data(1,:),'y',data(1,:),'g',data(1,:),'r',data(1,:),'c','LineWidth',2);
%axis([1 bins_per_second*1.5+1 -.05 1.05 ]);

ylabel('Match strength','FontSize',16,'Color','w')
xlabel('T (msec)','FontSize',16,'Color','w')

set(gca,'XTick',[1:pool_length_in_bins/4:pool_length_in_bins],'FontSize',14,'XColor',[1 1 1])
set(gca,'XTickLabel',{'0','500','1000','1500','2000'},'XColor',[1 1 1])

set(gca,'YTick',0:.2:1,'FontSize',14,'YColor','w')
disp(['N=' num2str(size(pool_start_bins,2))]);
%legend({'Match response', 'Mismatch'},'FontSize',14);

set(gcf, 'color', 'k');
set(gca, 'color', 'k');
set(gcf, 'InvertHardCopy', 'off');

